package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.OptionsMapper;
import com.tanhua.model.domain.Answers;
import com.tanhua.model.domain.Options;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pc
 * @date 2021/11/27
 */
@DubboService
public class OptionsApiImpl implements OptionsApi {
    @Autowired
    private OptionsMapper optionsMapper;



    /**
     * 获取问卷分数
     * @param answers
     * @return
     */
    @Override
    public Integer getScore(List<Answers> answers) {
        List<String> optionIds = new ArrayList<>();
        for (Answers answer : answers) {
            String optionId = answer.getOptionId();
            optionIds.add(optionId);
        }
        List<Options> optionsList = optionsMapper.selectBatchIds(optionIds);
        Integer score = 0;
        for (Options options : optionsList) {
            score += options.getScore();
        }

        return score;
    }

    /**
     * 获取试题选项
     * @param id
     * @return
     */
    @Override
    public List<Options> getOption(Integer id) {
        LambdaQueryWrapper<Options> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Options::getQuestionsId, id);
        return optionsMapper.selectList(lqw);
    }
}
